﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness.Service;
using Ext.Net;
using Bussiness;
using Bussiness.DAL.Entyti_Bussiness;

namespace Bussiness.View.BackEnd
{
    public partial class MG_Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Appcontex app = (Appcontex)Session["app"];
            if (app.IsAdmin == false) them.Visible = false;

            string action = Request.Params.Get("Action");
            string id = Request.Params.Get("ID");
            if (action != null && id != null)
            {
                if (action == "Delete")
                {
                    if (app.IsAdmin == false)
                    {
                        MessageBox mgb = new MessageBox();
                        mgb.Alert("Lỗi","Bạn không có quyền xóa sản phẩm này.");
                        mgb.Show();
                    }
                    else delete_Product(int.Parse(id));

                }
            }
            if (!X.IsAjaxRequest)
            {
                getAll_Product_Catelogies();
            }
        }

        public void getAll_Product_Catelogies()
        {
           // if(new Product_Service().getAll_CATELOGIES_PRODUCTS() != null){
            LoaiSP_Store.DataSource = new Catelogy_Service().getAllCatelogiesProductChild();
            LoaiSP_Store.DataBind();


           // }
        }

        public void search_Product(object sender, DirectEventArgs e)
        {
            search();
        }

        
        public void delete_Product(int id)
        {
            new Product_Service().delete_Product(id);
            search();
        }


        public void search()
        {
            Product pr = new Product();
            pr.Name = tk_tenHH.Text;
            pr.Code = tk_maHang.Text;
            if (tk_sale.Checked == true)
                pr.Sale = 1;
            if (!Formater.isNull(tk_soLuong.Text))
                pr.Count = int.Parse(tk_soLuong.Text);

            if (!Formater.isNull(LoaiSP_Grid.SelectedItem.Value))
                pr.IdCategory = int.Parse(LoaiSP_Grid.SelectedItem.Value);

            if (tk_NgayNhap.IsEmpty == false)
                pr.DateImport = DateTime.Parse(tk_NgayNhap.Text);
            else
                pr.DateImport = null;



            DSHangHoa_Store.DataSource = new Product_Service().search_Product(pr);
            DSHangHoa_Store.DataBind();
        }
    }
}
